using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using ZoneFiveSoftware.Common.Data;
using ZoneFiveSoftware.Common.Data.Fitness;
using ZoneFiveSoftware.Common.Data.Measurement;
using ZoneFiveSoftware.Common.Visuals;
using ZoneFiveSoftware.Common.Visuals.Chart;
using FilteredStatistics.Common;
using FilteredStatistics.Controller;
using System.Diagnostics;

namespace FilteredStatistics.Common.Controller
{
    class CadenceDataTrackProvider : IActivityDataTrackProvider
    {
        public CadenceDataTrackProvider()
        {
            PluginMain.ZoneCategoryListChanged += new PluginMain.ZoneCategoryChangedEventHandler(OnZoneCategoryChanged);
            m_ActivityDataChangedHelper.PropertyChanged += new PropertyChangedEventHandler(OnActivityDataChanged);
        }

        void OnZoneCategoryChanged(object sender, IZoneCategory category)
        {
            if (DataTrackChanged != null)
            {
                DataTrackChanged(this, new PropertyChangedEventArgs("AxisStripes"));
            }
        }

        void OnActivityDataChanged(object sender, PropertyChangedEventArgs e)
        {
            if (DataTrackChanged != null)
            {
                bool trackNeedsRefresh = false;
                bool axisNeedsRefresh = false;
                bool stripesNeedsRefresh = false;

                if (e.PropertyName == "Activity.CadencePerMinuteTrack")
                {
                    trackNeedsRefresh = true;
                }
                else if (e.PropertyName == "Activity.Duration")
                {
                    trackNeedsRefresh = true;
                }
                else if (e.PropertyName == "AnalysisSettings.CadenceSmoothingSeconds")
                {
                    trackNeedsRefresh = true;
                }

                // Throw events
                if (trackNeedsRefresh)
                {
                    DataTrackChanged(this, new PropertyChangedEventArgs("SmoothedDataTrack"));
                }

                if (axisNeedsRefresh)
                {
                    DataTrackChanged(this, new PropertyChangedEventArgs("Axis"));
                }

                if (stripesNeedsRefresh)
                {
                    DataTrackChanged(this, new PropertyChangedEventArgs("AxisStripes"));
                }
            }
        }

#region IActivityDataTrackProvider Members

        public Guid ReferenceId
        {
            get { return new Guid("87d9df83-98bc-4564-b625-87e600e08ab4"); }
        }

        public bool IsPrimaryDataTrack
        {
            get { return true; }
        }

        public IActivity Activity
        {
            set
            {
                if (m_Activity != value)
                {
                    m_Activity = value;
                    m_ActivityDataChangedHelper.Activity = value;
                }
            }
        }

        public string YAxisLabel
        {
            get
            {
                return CommonResources.Text.LabelCadence;
            }
        }

        public string YAxisUnitLabel
        {
            get
            {
                return "(" + CommonResources.Text.LabelRPM + ")";
            }
        }

        public Color TrackColor
        {
            get { return Color.FromArgb(78, 154, 6); }
        }

        public IAxisFormatter AxisFormatter 
        {
            get
            {
                return new Formatter.General();
            }
        }

        public INumericTimeDataSeries SmoothedDataTrack
        {
            get
            {
                if (m_Activity != null)
                {
                    return ActivityInfoCache.Instance.GetInfo(m_Activity).SmoothedCadenceTrack;
                }

                return null;
            }
        }

        public IList<Guid> DataTrackZoneGuids
        {
            get
            {
                List<Guid> result = new List<Guid>();

                foreach (IZoneCategory category in PluginMain.GetApplication().Logbook.CadenceZones)
                {
                    result.Add(new Guid(category.ReferenceId));
                }

                return result;
            }
        }

        public Guid DefaultZone
        {
            get
            {
                return new Guid(PluginMain.GetApplication().Logbook.CadenceZones[0].ReferenceId);
            }
        }

        public string GetDataTrackZoneName(Guid zoneId)
        {
            foreach (IZoneCategory category in PluginMain.GetApplication().Logbook.CadenceZones)
            {
                if (category.ReferenceId.Equals(zoneId.ToString()))
                {
                    return category.Name;
                }
            }

            return String.Empty;
        }

        public IList<AxisStripe> GetDataTrackZones(Guid zoneId)
        {
            List<AxisStripe> result = new List<AxisStripe>();

            foreach (IZoneCategory category in PluginMain.GetApplication().Logbook.CadenceZones)
            {
                if (category.ReferenceId.Equals(zoneId.ToString()))
                {
                    foreach (INamedLowHighZone zone in category.Zones)
                    {
                        double lower = zone.Low;
                        double upper = zone.High;

                        AxisStripe newStripe = new AxisStripe(lower, upper, Color.FromArgb(16, 0, 0, 0));

                        newStripe.Name = zone.Name;
                        result.Add(newStripe);
                    }
                }
            }

            return result;
        }

        public event PropertyChangedEventHandler DataTrackChanged;

#endregion

        private IActivity m_Activity = null;
        private ActivityDataChangedHelper m_ActivityDataChangedHelper = new ActivityDataChangedHelper(null);
    }
}
